package com.app.micaihu.custom.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.configure.Config;
import com.app.micaihu.manager.UserInforManager;
import com.app.utils.util.ViewSizeUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;

/**
 * 账户下线弹窗
 * Created by Guo on 2016/8/5.
 */
public class ErrorTokenDialog extends Dialog implements View.OnClickListener{


    private Activity activity;

    public ErrorTokenDialog(Activity context) {
        super(context,R.style.CustomDialogStyle);
        this.activity = context;
        initView();
    }

    public ErrorTokenDialog(Activity context, int themeResId) {
        super(context, themeResId);
        this.activity = context;
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_remotelogin,null);

        TextView btnNegative = (TextView) view.findViewById(R.id.btn_negative);
        TextView btnPositive = (TextView) view.findViewById(R.id.btn_positive);

        if(btnNegative != null) btnNegative.setOnClickListener(this);
        if(btnPositive != null) btnPositive.setOnClickListener(this);



        requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
        setCanceledOnTouchOutside(false);//不允许点击屏幕取消弹窗
        setContentView(view);

    }


    public void show() {
        if(activity != null && !activity.isFinishing() && !isShowing()){
            try{
                WindowManager.LayoutParams attributes = this.getWindow().getAttributes();
                attributes.width = ViewSizeUtils.dp2px(activity,300);
                attributes.height = ViewSizeUtils.dp2px(activity,135);
                this.getWindow().setAttributes(attributes);
                super.show();
            }catch (Exception e){
            }

        }
    }


    @Override
    public void onClick(View v) {
        if(v == null) return;
        switch(v.getId()){
            case R.id.btn_negative:
                dismissThis();
                LiveEventBus.get(Config.EventBus.taskSuccess).post(true);
                break;
            case R.id.btn_positive:
                dismissThis();
                if(activity != null) UserInforManager.getInstance().toLoginPage(activity);
                break;
        }
    }




    private void dismissThis(){
        if(activity != null && !activity.isFinishing()){
            this.dismiss();
        }
    }



    public Activity getActivity(){
        return activity;
    }


}
